<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission"/>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item v-if="modification" label="员工ID" prop="employeeId">
            <el-select v-model="form.employeeId" placeholder="请选择" style="width: 370px;" filterable clearable>
              <el-option
                v-for="item in teamGroups"
                :key="item.user_id"
                :label="item.username"
                :value="`${item.username}|${item.user_id}`">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否是负责人" prop="isPrincipal">
            <el-select v-model="form.isPrincipal" filterable placeholder="请选择" style="width: 370px;">
              <el-option
                v-for="item in dict.whether"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="employeeId" label="员工ID"/>
        <el-table-column prop="employeeName" label="员工姓名"/>
        <el-table-column prop="isPrincipal" label="是否是负责人">
          <template slot-scope="scope">
            {{ dict.label.whether[scope.row.isPrincipal] }}
          </template>
        </el-table-column>
        <el-table-column prop="createBy" label="创建人"/>
        <el-table-column prop="createTime" label="创建时间"/>
        <el-table-column prop="updateBy" label="更新人"/>
        <el-table-column prop="updateTime" label="更新时间"/>
        <el-table-column v-if="checkPer(['admin','factoryTeamEmployee:edit','factoryTeamEmployee:del'])" label="操作"
                         width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
  import crudFactoryTeamEmployee from '@/api/factory/factoryTeamEmployee'
  import CRUD, {presenter, header, form, crud} from '@crud/crud'
  import rrOperation from '@crud/RR.operation'
  import crudOperation from '@crud/CRUD.operation'
  import udOperation from '@crud/UD.operation'
  import pagination from '@crud/Pagination'

  const defaultForm = {
    id: null,
    teamGroupId: null,
    employeeId: null,
    isPrincipal: "0",
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    employeeName:null
  }
  export default {
    props: {
      id: {
        type: Number,
      },
    },
    watch: {
      id(value) {
        this.crud.toQuery();
      }
    },
    name: 'FactoryTeamEmployee',
    components: {pagination, crudOperation, rrOperation, udOperation},
    mixins: [presenter(), header(), form(defaultForm), crud()],
    dicts: ['whether'],
    cruds() {
      return CRUD({
        title: '班组人员',
        url: 'api/factoryTeamEmployee',
        idField: 'id',
        sort: 'id,desc',
        optShow: {
          add: true,
          edit: false,
          del: false,
          download: false,
          reset: true,
          import: false
        },
        crudMethod: {...crudFactoryTeamEmployee}
      })
    },
    data() {
      return {
        teamGroups: null,
        modification:true,
        permission: {
          add: ['admin', 'factoryTeamEmployee:add'],
          edit: ['admin', 'factoryTeamEmployee:edit'],
          del: ['admin', 'factoryTeamEmployee:del']
        },
        rules: {
          employeeId: [
            {required: true, message: '员工ID不能为空', trigger: 'blur'}
          ],
          isPrincipal: [
            {required: true, message: '是否是负责人不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      // 钩子：在获取表格数据之前执行，false 则代表不获取数据
      [CRUD.HOOK.beforeToAdd]() {
        crudFactoryTeamEmployee.getUser().then(res => {
          this.teamGroups = res
        });
        this.form.teamGroupId = this.id
        return true
      },
      //获取数据之前
      [CRUD.HOOK.beforeRefresh]() {
        this.crud.params.teamGroupId = this.id;
        return true
      },
      //提交之前
      [CRUD.HOOK.beforeSubmit]() {
        if (this.modification) {
          var ts = this.form.employeeId.split("|")
          this.form.employeeId = ts[1]
          this.form.employeeName=ts[0]
        }
        return true
      },
      //编辑前
      [CRUD.HOOK.beforeToEdit]() {
        this.modification = false
        return true;
      },
      //提交后
      [CRUD.HOOK.afterSubmit]() {
        this.modification = true
        return true;
      },
      //编辑取消之后
      [CRUD.HOOK.afterEditCancel]() {
        this.modification = true
        return true;
      },


    },
    mounted() {

    }
  }
</script>

<style scoped>

</style>
